package r5.powtorka2;

import java.text.DateFormat;
import java.util.Date;
import java.util.GregorianCalendar;

public class Employee extends Person implements Comparable<Employee> {

	private static int nextID;
	static {
		nextID = 1;
	}
	private double salary;
	private Date hireDay;
	private int id;

	public Employee(String name, double salary, int year, int month, int day) {
		this.name = name;
		this.salary = salary;
		this.hireDay = new GregorianCalendar(year, month + 1, day).getTime();
		this.id = getID();
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (!(obj instanceof Employee))
			return false;
		Employee other = (Employee) obj;
		if (hireDay == null) {
			if (other.hireDay != null)
				return false;
		} else if (!hireDay.equals(other.hireDay))
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (Double.doubleToLongBits(salary) != Double
				.doubleToLongBits(other.salary))
			return false;
		return true;
	}

	public Date getHireday() {
		return (Date) hireDay.clone();
	}

	public int getId() {
		return id;
	}

	public double getSalary() {
		return salary;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((hireDay == null) ? 0 : hireDay.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		long temp;
		temp = Double.doubleToLongBits(salary);
		result = prime * result + (int) (temp ^ (temp >>> 32));
		return result;
	}

	public void raisesalary(double percent) {
		this.salary += this.salary * percent / 100;
	}

	public String toString() {
		return this.getClass().getSimpleName() + " " + this.name
				+ ", zatrudniony "
				+ DateFormat.getDateInstance().format(this.hireDay)
				+ ", pensja: " + this.salary;
	}

	private int getID() {
		return Employee.nextID++;
	}

	@Override
	public int compareTo(Employee e) {
		if (this.salary > e.salary)
			return 1;
		else if (this.salary < e.salary)
			return -1;
		else
			return 0;
	}

	@Override
	public Object clone() throws CloneNotSupportedException {
		Employee cloned = (Employee) super.clone();
		cloned.hireDay = (Date) this.hireDay.clone();
		return cloned;
		
	}

}
